$(function() {
    _editor = UE.getEditor('upload_ue',{
        serverUrl:"/file/upload.do?compress=1"
    });
    _editor.ready(function () {
        _editor.hide();
        _editor.addListener('beforeInsertImage', function (t, arg) {
            $("#img").val(arg[0].src);
            $("#classimg").attr("src",arg[0].src);
        });
    });
});

var flag = "add";
//打开添加基础科室弹层
function openEditBox(type){
    flag=type;
    $("#editbaseForm").form("clear");
    if(type=="add"){
        $("#baselogo").attr("src","");
    }else{
        var select = $("#deparment_datagrid").datagrid("getSelected");
        if(select==null){$.messager.alert("提示","请选择一个科室","info");return;}
        $("#editbaseForm").form("load",select);
        $("#baselogo").attr("src",select.logo);
    }
    $("#editBase_dialog").dialog("open");
}
//打开添加机构科室弹层
function openShopDeptBox(type){
    flag=type;
    $("#editDeptForm").form("clear");
    $('#selcardcat1').combobox({
        url : "/sellerDept/baseDeptcb.do",
        valueField:'id',
        textField:'name',
        panelHeight : 'auto',
        editable:false
    });
    if(type=="add"){
        var select = $("#seller_datagrid").datagrid("getSelected");
        if(select==null){$.messager.alert("提示","请选择一个机构","info");return;}
        $("#editDept_dialog").dialog({title: "添加"});
        $("#editDeptForm").find(".shopName").text(select.name);
        $("#editDeptForm").find("[name='sellerId']").val(select.id);
    }else{
        var select = $("#sellerDept_datagrid").datagrid("getSelected");
        if(select==null){$.messager.alert("提示","请选择一个机构科室","info");return;}
        $("#editDept_dialog").dialog({title: "修改"});
        $("#editDeptForm").find(".shopName").text(select.sellerName);
        $("#editDeptForm").form("load",select);
    }
    $("#editDept_dialog").dialog("open");
}
//保存或修改基础科室
function editDept(){
    $("#editbaseForm").form("submit",{
        url:"/sellerDept/addOredit.do",
        onSubmit:function(){
            var valid = $(this).form("validate");
            if (!valid) {
                $.messager.progress("close");
            }
            return valid;
        },
        success:function(data){
            data=$.parseJSON(data);
            $.messager.progress("close");
            if(data.success){
                $("#editBase_dialog").dialog("close");
                $.messager.show({
                    title: "成功",msg:"操作成功!",
                    timeout:600,style:"left:30%;top:10%",
                    showType:"fade"
                });
                $("#deparment_datagrid").datagrid("reload");
            }else{
                $.messager.alert( "失败", data.msg,"error");
            }
        }
    });
}
//保存或修改机构科室
function editShopDept(){
    $("#editDeptForm").form("submit",{
        url:"/sellerDept/addShopdept.do",
        onSubmit:function(){
            var valid = $(this).form("validate");
            if (!valid) {
                $.messager.progress("close");
            }
            return valid;
        },
        success:function(data){
            data=$.parseJSON(data);
            $.messager.progress("close");
            if(data.success){
                $("#editDept_dialog").dialog("close");
                $.messager.show({
                    title: "成功",msg:"操作成功!",
                    timeout:600,style:"left:30%;top:10%",
                    showType:"fade"
                });
                $("#sellerDept_datagrid").datagrid("reload");
            }else{
                $.messager.alert( "失败", data.msg,"error");
            }
        }
    });
}
//编辑科室简介和荣誉
function updateInfo( rowIndex,field, value){
   console.log(rowIndex);
   console.log(field);
   console.log(value);
}
function deleteBase() {
    var selectrow = $("#deparment_datagrid").datagrid("getSelected");
    if(selectrow==null){$.messager.alert( "提示", "请选择要删除的科室","info");return;}
    $.messager.confirm("确认","确定要删除该基础科室吗?\n确定删除将连同同名机构科室信息一同删除",function(yes){
        if(yes){
            commonDelete("/sellerDept/del.do",selectrow.id,"deparment_datagrid")
        }
    });
}
//删除机构科室
function delShopDept() {
    var rowselect  = $("#sellerDept_datagrid").datagrid("getSelected");
    if(rowselect==null){$.messager.alert( "提示", "请选择要删除的科室","info");return;}
    $.messager.confirm("确认","确定要删除该种类吗?",function(yes){
        if(yes){
            commonDelete("/sellerDept/delShopDept.do",rowselect.id,"sellerDept_datagrid")
        }
    });
}
//搜索机构
function searchSeller(value){
    var keyword = $("#searchSeller").val();
    var queryParams = {};
    queryParams.sellerName = keyword;
    var options = $("#seller_datagrid").datagrid('getPager').data("pagination").options;
    options.pageNumber = 1;
    $("#seller_datagrid").datagrid('options').queryParams = queryParams;
    $("#seller_datagrid").datagrid('reload');
}
//联动处理
function changeShoprow(id){
    var queryParams = {};
    queryParams.sellerId = id;
    var options = $("#sellerDept_datagrid").datagrid('getPager').data("pagination").options;
    options.pageNumber = 1;
    $("#sellerDept_datagrid").datagrid('options').queryParams = queryParams;
    $("#sellerDept_datagrid").datagrid('reload');
}
//重置
function refcardSellerdept(){
    var queryParams = {};
    var options = $("#sellerDept_datagrid").datagrid('getPager').data("pagination").options;
    options.pageNumber = 1;
    $("#sellerDept_datagrid").datagrid('options').queryParams = queryParams;
    $("#sellerDept_datagrid").datagrid('reload');
}
//弹出图片上传的对话框
function upImage() {
    var myImage = _editor.getDialog("insertimage");
    myImage.open();
}
//删除公共调用方法
function commonDelete(url,id,datagrid){
    $.messager.progress();
    $.ajax({
        url:url,
        type:"post",
        data:{id : id},
        dataType:"json",
        success:function(data){
            $.messager.progress("close");
            if(data.success){
                $.messager.show({
                    title:"删除成功",
                    msg:"删除成功!",
                    timeout:600,
                    style:"left:30%;top:10%",
                    showType:"fade"
                });
                $("#"+datagrid).datagrid("reload");
                $("#"+datagrid).datagrid('clearSelections');
            }else{
                $.messager.alert("删除失败", data.msg,"error");
            }
        },
        error:function(){
            $.messager.progress("close");
            $.messager.alert("删除失败", "服务器连接失败!","error");
        }
    });
}